package com.xutong.myfunction.custom;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xutong.myfunction.R;
import com.xutong.myfunction.entity.ImageItem;

import java.util.List;

/**
 * Created by hasee on 2016/9/14.
 */
public class CustomHorSor {


    /**
     * 设置gridView
     * @param mContext
     * @param gridView gridView控件
     * @param mjImageList 数据 （这个是默认数据类型，如需自己定义，同时也需要更改适配器）
     * @param defaultSum 默认可见数量
     */
    public static void setGridView(Context mContext, GridView gridView, List<ImageItem> mjImageList, int defaultSum) {
        int size = mjImageList.size();
        int length = 100;
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();

//        getWindowManager().getDefaultDisplay().getMetrics(dm);
//        float density = dm.density;

        length = dm.widthPixels / defaultSum;
        int gridviewWidth = (length + defaultSum) * size;
        int itemWidth = length;

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
        gridView.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        gridView.setColumnWidth(itemWidth); // 设置列表项宽
//        gridView.setHorizontalSpacing(5); // 设置列表项水平间距
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setNumColumns(size); // 设置列数量=列表集合数
        gridView.setSelector(R.color.white);

        GridViewAdapter adapter = new GridViewAdapter(mContext, mjImageList);
        gridView.setAdapter(adapter);

    }


    /**
     * GirdView 数据适配器
     */
    public static class GridViewAdapter extends BaseAdapter {
        Context context;
        List<ImageItem> list;

        public GridViewAdapter(Context _context, List<ImageItem> _list) {
            this.list = _list;
            this.context = _context;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.item_main_grid1, null);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.ivZhiNengMenJin);
            TextView tvdesc = (TextView) convertView.findViewById(R.id.tvZhiNengMenJin);

            imageView.setImageResource(list.get(position).getimageId());
            tvdesc.setText(list.get(position).getImageDesc());
            return convertView;
        }
    }


}
